home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CTOOLS10.ARJ / AVL.H < prev    next >
C/C++ Source or Header  |  1991-12-31  |  3KB  |  105 lines

  1. /****************************************************************************
  2. *
  3. *                    Copyright (C) 1991 Kendall Bennett.
  4. *                            All rights reserved.
  5. *
  6. * Filename:        $RCSfile: avl.h $
  7. * Version:        $Revision: 1.3 $
  8. *
  9. * Language:        ANSI C
  10. * Environment:    any
  11. *
  12. * Description:    Header file for AVL tree module.
  13. *
  14. * $Id: avl.h 1.3 91/12/31 19:41:40 kjb Exp $
  15. *
  16. * Revision History:
  17. * -----------------
  18. *
  19. * $Log:    avl.h $
  20. * Revision 1.3  91/12/31  19:41:40  kjb
  21. * Modified include file directories.
  22. * Revision 1.2  91/09/27  03:11:19  kjb
  23. * Added compatibility with C++.
  24. * Revision 1.1  91/09/27  02:50:21  kjb
  25. * Initial revision
  26. ****************************************************************************/
  27.  
  28. #ifndef    __AVL_H
  29. #define    __AVL_H
  30.  
  31. #ifndef    __DEBUG_H
  32. #include "debug.h"
  33. #endif
  34.  
  35. /*---------------------- Macros and type definitions ----------------------*/
  36.  
  37. typedef struct AVL_BUCKET {
  38.     struct AVL_BUCKET    *left;        /* Pointer to left subtree            */
  39.     struct AVL_BUCKET    *right;        /* Pointer to right subtree            */
  40.     short                bal;        /* Balance factor                    */
  41.     } AVL_BUCKET;
  42.  
  43. typedef struct {
  44.     int            count;            /* Number of elements currently in tree    */
  45.     int            (*cmp)(void*,void*);    /* Compare two nodes            */
  46.     AVL_BUCKET    *root;            /* Pointer to root node of AVL tree        */
  47.     } AVL_TREE;
  48.  
  49. /* The three traversal orders supported    */
  50.  
  51. #define    PREORDER    0
  52. #define    INORDER        1
  53. #define    POSTORDER    2
  54.  
  55. #define    MAXLEVEL    64            /* Maximum levels for avl_print            */
  56.  
  57. /* The three balance factors stored in each subtree                        */
  58.  
  59. #define    LH        0                /* Left subtree is larger                */
  60. #define    BAL        1                /* Subtree is balanced                    */
  61. #define    RH        2                /* Right subtree is balanced            */
  62.  
  63. /* Return a pointer to the user space given the address of the header of
  64.  * a node.
  65.  */
  66.  
  67. #define    AVL_USERSPACE(h)    ((void*)((AVL_BUCKET*)(h) + 1))
  68.  
  69. /* Return a pointer to the header of a node, given the address of the
  70.  * user space.
  71.  */
  72.  
  73. #define    AVL_HEADER(n)        ((AVL_BUCKET*)(n) - 1)
  74.  
  75. #define    AVL_EMPTY(t)        ((t)->count == 0)
  76.  
  77. /*-------------------------- Function Prototypes --------------------------*/
  78.  
  79. #ifdef __cplusplus
  80. extern "C" {
  81. #endif
  82.  
  83. void *avl_newnode(int size);
  84. void avl_freenode(void *node);
  85. AVL_TREE *avl_init(int (*cmp_function)());
  86. void avl_empty(AVL_TREE *t,void (*freeNode)());
  87. void *avl_insert(AVL_TREE *tree,void *node);
  88. void *avl_delete(AVL_TREE *tree,void *node);
  89. void avl_traverse(AVL_TREE *tree,int order,void (*visit)(),void *params);
  90. void avl_print(FILE *out,AVL_TREE *tree,void (*prnt)(),bool graph_chars);
  91. void *avl_findsym(AVL_TREE *tree,void *node);
  92. void avl_range(AVL_TREE *tree,void *lower,void *upper,void (*visit)(),
  93.     void *params);
  94. void *avl_delmin(AVL_TREE *tree);
  95. void *avl_delmax(AVL_TREE *tree);
  96.  
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100.  
  101. #endif
  102.